package org.mogu.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.mogu.db.ActiveUserHelper;
import org.mogu.staticvalue.Convert;
import org.mogu.tools.UrlSync;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class GetSyncUrl {
	SQLiteDatabase readdb = null;
	ActiveUserHelper auh = null;
	DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public GetSyncUrl(ActiveUserHelper a){
		auh=a;
		readdb=auh.getReadableDatabase();
	}
	public SQLiteDatabase getReaddb(){
		if(readdb==null&&!readdb.isOpen()){
			readdb=auh.getReadableDatabase();
		}
		return readdb;
	}
	
	public void getAppUrl(){
		String[] appAr = { Convert.uname };
		Map<String, ArrayList<String>> appMap=new HashMap<String, ArrayList<String>>();
		synchronized (InfoSync.class) {
		Cursor app = getReaddb().rawQuery(
				"select code,url from app where username=?", appAr);

		app.moveToFirst();
		while (!app.isAfterLast()) {
			if (!appMap.containsKey(app.getString(0))) {
				appMap.put(app.getString(0), new ArrayList<String>());
			}
			appMap.get(app.getString(0)).add(app.getString(1));
			app.moveToNext();
		}
		app.close();
		Convert.appMap=appMap;
		}
	}
	
	public static String getAppUrlByCode(String code){
		if(Convert.appMap.get(code).size()==0){
			return "";
		}
		return Convert.appMap.get(code).get(new Date().getSeconds()% Convert.appMap.get(code).size());
	}
	
	public  void getAppParam() {
		String[] appAr = { Convert.uname };
		synchronized (InfoSync.class) {
			Cursor app = getReaddb().rawQuery(
					"select appparam from activeuser where username=?", appAr);
			
			app.moveToFirst();
			while (!app.isAfterLast()) {
				
				Convert.appparam=app.getString(0);
				app.moveToNext();
			}
			app.close();
		}
	}
	public  void getUserParam() {
		String[] appAr = { Convert.uname };
		Map<String, String> userAappMap = new HashMap<String, String>();
		userAappMap.put("main", "");
		synchronized (InfoSync.class) {
			Cursor app = getReaddb().rawQuery(
					"select code,appparm from userapp where username=?", appAr);
			
			app.moveToFirst();
			while (!app.isAfterLast()) {
				userAappMap.put(app.getString(0), app.getString(1));
				app.moveToNext();
			}
			app.close();
			Convert.userAappMap=userAappMap;
		}
	}
	
	public static String getUserParamByCode(String code){
		String appParm=Convert.userAappMap.get(code);
		if(appParm==null){
			appParm="";
		}
		return Convert.appparam+appParm;
	}
	
	public void getNeedSyncUrl(){
//		if(Convert.syncList.size()>3){
//			return;
//		}
		ArrayList<String> codeList;
		String infoall="/InfoAll";
		String infoupdate="/InfoUpdate";
		synchronized (InfoSync.class) {
		String[] appAr = { Convert.uname };
		
		UrlSync urlSync;
		Cursor imglibcursor = getReaddb()
				.rawQuery(
						"select code,infoallUpdateTime,infoallTimeNum,infoupdateUpdateTime,infoupdateTimeNum from userapp where username=?",
						appAr);
		imglibcursor.moveToFirst();
		String code;
		String infoallUpdateTime;
		int infoallTimeNum;
		String infoupdateUpdateTime;
		int infoupdateTimeNum;
		Date currentNow=new Date();
		codeList=new ArrayList<String>();
		while(!imglibcursor.isAfterLast()){
			code=imglibcursor.getString(0);
			codeList.add(code);
			infoallUpdateTime=imglibcursor.getString(1);
			infoallTimeNum=imglibcursor.getInt(2);
			infoupdateUpdateTime=imglibcursor.getString(3);
			infoupdateTimeNum=imglibcursor.getInt(4);
			if(Convert.appMap.containsKey(code)){
				try {
					if (infoallUpdateTime == null
							|| "".equals(infoallUpdateTime)
							|| (currentNow.getTime() - Convert.format1.parse(infoallUpdateTime).getTime())>= infoallTimeNum * 1000) {
						urlSync=new UrlSync();
						urlSync.setNotice(false);
						urlSync.setUri(GetSyncUrl.getAppUrlByCode(code)+infoall);
						urlSync.setMaincode(code);
						urlSync.setSyncType(UrlSync.INFOALL);
						addList(urlSync);
						
					}
				} catch (ParseException e) {
					urlSync=new UrlSync();
					urlSync.setNotice(false);
					urlSync.setUri(GetSyncUrl.getAppUrlByCode(code)+infoall);
					urlSync.setMaincode(code);
					urlSync.setSyncType(UrlSync.INFOALL);
					addList(urlSync);
				}
				try {
					if (infoupdateUpdateTime == null
							|| "".equals(infoupdateUpdateTime)
							|| (currentNow.getTime() - Convert.format1.parse(infoupdateUpdateTime).getTime())>= infoupdateTimeNum * 1000) {
						urlSync=new UrlSync();
						urlSync.setNotice(true);
						urlSync.setUri(GetSyncUrl.getAppUrlByCode(code)+infoupdate);
						urlSync.setMaincode(code);
						urlSync.setSyncType(UrlSync.INFOUPDATE);
						addList(urlSync);
						
					}
				} catch (ParseException e) {
					urlSync=new UrlSync();
					urlSync.setNotice(true);
					urlSync.setUri(GetSyncUrl.getAppUrlByCode(code)+infoupdate);
					urlSync.setMaincode(code);
					urlSync.setSyncType(UrlSync.INFOUPDATE);
					addList(urlSync);
				}
			}else{
				urlSync=new UrlSync();
				urlSync.setNotice(false);
				urlSync.setUri(GetSyncUrl.getAppUrlByCode(code)+infoall);
				urlSync.setMaincode(code);
				urlSync.setSyncType(UrlSync.INFOALL);
				addList(urlSync);
				
				urlSync=new UrlSync();
				urlSync.setNotice(true);
				urlSync.setUri(GetSyncUrl.getAppUrlByCode(code)+infoupdate);
				urlSync.setMaincode(code);
				urlSync.setSyncType(UrlSync.INFOUPDATE);
				addList(urlSync);
				
			}
			imglibcursor.moveToNext();
			
		}
		imglibcursor.close();
		}
		UrlSync urlSync;
		for (String maincode : Convert.appMap.keySet()) {
			if(!codeList.contains(maincode)){
				urlSync=new UrlSync();
				urlSync.setNotice(false);
				urlSync.setUri(GetSyncUrl.getAppUrlByCode(maincode)+infoall);
				urlSync.setMaincode(maincode);
				urlSync.setSyncType(UrlSync.INFOALL);
				addList(urlSync);
				
				urlSync=new UrlSync();
				urlSync.setNotice(true);
				urlSync.setUri(GetSyncUrl.getAppUrlByCode(maincode)+infoupdate);
				urlSync.setMaincode(maincode);
				urlSync.setSyncType(UrlSync.INFOUPDATE);
				addList(urlSync);
			}
		}
	}
	
	public void addList(UrlSync urlSync){
		boolean has=false;
		for (UrlSync u : Convert.syncList) {
			if(urlSync.getAllUri().equals(u.getAllUri())){
				has=true;
			}
		}
		if(!has){
			Convert.syncList.add(urlSync);
		}
	}
	
	public void init(){
		this.getAppUrl();
		this.getAppParam();
		this.getUserParam();
		this.getNeedSyncUrl();
	}

}
